package com.example.ljw.frescowallpaper.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.bean.ConstellationBean;
import com.example.ljw.frescowallpaper.fragment.WallFragment;
import com.example.ljw.frescowallpaper.utils.IMyGirdListener;
import com.example.ljw.frescowallpaper.utils.RecyclerViewAdapterHelper;
import com.example.ljw.frescowallpaper.view.Details;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by LJW on 2016/11/20.
 */

public class MyConstellationAdapter extends RecyclerViewAdapterHelper<Object> {
    //设置3种布局
    public static final int TYPE1=1,TYPE2=2,TYPE3=3;
    private  ConstellationBean.DataBean.AstroBean astroBean=null;
    private  ConstellationBean.DataBean.WallpaperBlogsBean  blogsBean=null;
    //传item数量
    @Override
    public int getItemCount() {
        return 8;
    }

    public MyConstellationAdapter(Context context, List<Object> list) {
        super(context, list);
        //头
        astroBean= (ConstellationBean.DataBean.AstroBean) mList.get(0);

    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE1;
        }else if(position==1||position==8){
            return TYPE2;
        }else {
            return TYPE3;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view=null;
        switch (viewType){
            case TYPE1:
                view=mInflater.inflate(R.layout.item_constellation_head,parent,false);
                return new ViewHolder1(view, (ConstellationBean.DataBean.AstroBean) mList.get(0));
            case TYPE2:
                view=mInflater.inflate(R.layout.item_constellation_name,parent,false);
                return new ViewHolder2(view);
            case TYPE3:
                view=mInflater.inflate(R.layout.item_layout,parent,false);
                return new ViewHolder3(view);

        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        String head=astroBean.getContent();
        String color=astroBean.getLuckyColor();
        if(position>1&&position<=7){
            blogsBean= (ConstellationBean.DataBean.WallpaperBlogsBean) mList.get(position);
        }

        if(holder instanceof ViewHolder1){
            ViewHolder1 holder1= (ViewHolder1) holder;
            holder1.imageView1.setImageResource(R.mipmap.whitesheep);
            holder1.tv1.setText(head);
            holder1.tv2.setText("幸运色:"+color);
            holder1.tv3.setText(astroBean.getName());
            holder1.btn.setText("详情");
        }else if(holder instanceof  ViewHolder2){
            ViewHolder2 holder2= (ViewHolder2) holder;
            holder2.tv.setText("幸运色壁纸");
        }else if(holder instanceof  ViewHolder3){
            ViewHolder3 holder3= (ViewHolder3) holder;
            Glide.with(mContext)
                    .load(blogsBean.getPhoto().getPath())
                    .centerCrop()
                    .placeholder(R.mipmap.wallpaper)
                    .into(holder3.img);
        }

    }
    class ViewHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView imageView1;
        private TextView tv1;
        private TextView tv2;
        private Button btn;
        private TextView tv3;
        private  ConstellationBean.DataBean.AstroBean astroBeanholder;
        public ViewHolder1(View itemView,ConstellationBean.DataBean.AstroBean astroBeanholder)  {
            super(itemView);
            this.astroBeanholder=astroBeanholder;
            imageView1= (ImageView) itemView.findViewById(R.id.img_item_constellation_head);
            tv1= (TextView) itemView.findViewById(R.id.tvdetails_item_constellation_head);
            tv2= (TextView) itemView.findViewById(R.id.tvcolor_item_constellation_head);
            tv3= (TextView) itemView.findViewById(R.id.tvconame_item_constellation_head);
            btn= (Button) itemView.findViewById(R.id.btn_item_constellation_head);
            btn.setOnClickListener(this);
            tv3.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_item_constellation_head:
                    showoPopupWindow(view);
                    break;
                case R.id.tvconame_item_constellation_head:
                    showoPopupWindowSort(view);
            }



        }
        //星座的类型
        private void showoPopupWindowSort(View view) {
            View contentView=LayoutInflater.from(mContext).inflate(R.layout.girdlayout_popwid
            ,null);
            //初始化popupwindow
            final PopupWindow popupWindow=new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,true);
            popupWindow.setTouchable(true);
            popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    return false;
                }
            });
            //必须设置背景，不设置的话出不去
            popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.backgroud));

            popupWindow.showAsDropDown(view, 0,0,Gravity.CENTER);
            //初始化控件
            final TextView tvG1= (TextView) contentView.findViewById(R.id.tv1_gird);
            final TextView tvG2= (TextView) contentView.findViewById(R.id.tv2_gird);
            final TextView tvG3= (TextView) contentView.findViewById(R.id.tv3_gird);
            final TextView tvG4= (TextView) contentView.findViewById(R.id.tv4_gird);
            final TextView tvG5= (TextView) contentView.findViewById(R.id.tv5_gird);
            final TextView tvG6= (TextView) contentView.findViewById(R.id.tv6_gird);
            final TextView tvG7= (TextView) contentView.findViewById(R.id.tv7_gird);
            final TextView tvG8= (TextView) contentView.findViewById(R.id.tv8_gird);
            final TextView tvG9= (TextView) contentView.findViewById(R.id.tv9_gird);
            final TextView tvG10= (TextView) contentView.findViewById(R.id.tv10_gird);
            final TextView tvG11= (TextView) contentView.findViewById(R.id.tv11_gird);
            final TextView tvG12= (TextView) contentView.findViewById(R.id.tv12_gird);
            tvG12.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG12.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG11.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG11.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG10.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG10.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG9.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG9.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG8.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG7.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG6.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG5.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG4.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG3.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG2.getText()+"");
                    popupWindow.dismiss();
                }
            });
            tvG1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(tvG1.getText()+"");
                    popupWindow.dismiss();
                }
            });


        }



        //详情
        private void showoPopupWindow(View view) {
            //自定义一个布局
            View contentView= LayoutInflater.from(mContext).inflate(R.layout.popup_window
            ,null);
            //初始化控件
            TextView tv_1= (TextView) contentView.findViewById(R.id.tv1_popupwindow);
            TextView tv_2= (TextView) contentView.findViewById(R.id.tv2_popupwindow);
            TextView tv_3= (TextView) contentView.findViewById(R.id.tv3_popupwindow);
            TextView tv_4= (TextView) contentView.findViewById(R.id.tv4_popupwindow);
            TextView tv_5= (TextView) contentView.findViewById(R.id.tv5_popupwindow);
            TextView tv_6= (TextView) contentView.findViewById(R.id.tv6_popupwindow);
            TextView tv_7= (TextView) contentView.findViewById(R.id.tv7_popupwindow);
            TextView tv_8= (TextView) contentView.findViewById(R.id.tv8_popupwindow);
                /**
                 * name : 白羊座
                 * content : 本周总会遇到几件让你心花怒放的好事，在收获欣喜时别忘了回报那些曾经帮助过你的人，与更多人分享你的快乐，你则会变得更加快乐。
                 * lucky_astro : 摩羯座
                 * lucky_color : 日光黄
                 * lucky_day : 星期日
                 * avert_astro : 水瓶座
                 * start_time : 1471708800
                 * end_time : 1472227200
                 * love_status : 恋人说几个笑话本周还不足以让你开怀大笑，当收到恋人为你准备的惊喜时你就彻底hold不住了！单身和异性在一起说说笑笑好不热闹，但论及感情问题就还差一点火候。
                 */
            tv_1.setText(astroBeanholder.getName()+"一周运势");
            tv_2.setText("贵人星座："+astroBeanholder.getLuckyAstro());
            tv_3.setText("本周幸运星座："+astroBeanholder.getLuckyAstro());
            tv_4.setText("小人星座："+astroBeanholder.getAvertAstro());
            tv_5.setText("幸运色："+astroBeanholder.getLuckyColor());
            tv_6.setText("幸运日："+astroBeanholder.getLuckyDay());
            tv_7.setText("爱情："+astroBeanholder.getContent());
            tv_8.setText("总体："+astroBeanholder.getLoveStatus());

            //初始化popupwindow
            PopupWindow popupWindow=new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,true);
            popupWindow.setTouchable(true);
            popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    return false;
                }
            });
            //必须设置背景，不设置的话出不去
            popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.backgroud));

            popupWindow.showAsDropDown(view, 0,0,Gravity.CENTER);


        }
    }
    class ViewHolder2 extends RecyclerView.ViewHolder{
        private TextView tv;
        public ViewHolder2(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.tv_item_constellation_name);
        }
    }
    class ViewHolder3 extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView img;
        public ViewHolder3(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.img_item);
            img.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Intent intent=new Intent(mContext, Details.class);
            if(getAdapterPosition()>1&&getAdapterPosition()<=7){
                blogsBean= (ConstellationBean.DataBean.WallpaperBlogsBean) mList.get(getAdapterPosition());
            }
            intent.putExtra("URL",blogsBean.getPhoto().getPath());
            mContext.startActivity(intent);

        }
    }
}
